﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.Helper;
using com.nm.library;
using com.nm.library.Helper;
using DistributeAPI.Model;
using NHibernateVertica;
using george.Data;
using george.Helper;
using com.nm.db.Models.Hkerp;

namespace DistributeAPI
{
    /// <summary>
    ///到货通知单查询API
    /// </summary>
    public class ArrivalNoticeHandler : BaseHandler
    {
        #region 查询

        #region 获取列表

        [MustLogin]
        [Description("获取到货通知单列表")]
        [ReturnType(typeof(IList<PMArrNoticeModel>))]
        public ApiResult GetList(
            [ParamDescription("到货通知单号")] string billNo = "",
            [ParamDescription("货号")] string MaterialID = "",
            [ParamDescription("开始时间")] DateTime BeginDate = default(DateTime),
            [ParamDescription("结束时间")] DateTime EndDate = default(DateTime),
            [ParamDescription("开单时间或者生效时间,0为开单，1为生效")] string Time = "-1",
            [ParamDescription("页码")] int Size = 50,
            [ParamDescription("页数")] int Page = 1)
        {
            var db = new HKERPEntities();
            string companyid = UserInfo.UserCompanyId;
            var temp = db.KM_V_PMArrNotice.ToList().Where(p => p.CompanyID == companyid);
            //校验
            if (!string.IsNullOrWhiteSpace(billNo))
            {
                temp = temp.Where(p => p.BillNo == billNo);
            }
            if (!string.IsNullOrWhiteSpace(MaterialID))
            {
                temp = temp.Where(p => p.MaterialID == MaterialID);
            }

            //判断是否过滤时间
            if (Time != "-1")
            {
                if (BeginDate != default(DateTime) || EndDate != default(DateTime))
                {
                    if (BeginDate != default(DateTime))
                    {
                        BeginDate = BeginDate.SetHour(0).SetMinute(0).SetSecond(0);
                    }
                    if (EndDate != default(DateTime))
                    {
                        EndDate = EndDate.SetHour(23).SetMinute(59).SetSecond(59);
                    }
                }
                if (EndDate == default(DateTime))
                    EndDate = DateTime.Now.SetHour(23).SetMinute(59).SetSecond(59);

                //如果是开单时间
                if (Time == "0")
                {
                    temp = temp.Where(p => p.BillDate >= BeginDate && p.BillDate <= EndDate);
                }
                //如果是生效时间
                else if (Time == "1")
                {
                    temp = temp.Where(p => p.CheckDate >= BeginDate && p.CheckDate <= EndDate);
                }
            }
            var query = (from p in temp
                         group p by new
                         {
                             p.BillDate,
                             p.BillNo,
                             p.CNStateName,
                             p.CheckDate,
                             p.MaterialID,
                             p.SourceBillNo
                         } into g
                         select new
                         {
                             g.Key,
                             qty = g.Sum(p => p.Qty),
                             g
                         }).Select(t => new PMArrNoticeModel
                         {
                             BillDate = t.Key.BillDate,
                             CheckDate = t.Key.CheckDate,
                             BillNo = t.Key.BillNo,
                             CNStateName = t.Key.CNStateName,
                             MaterialID = t.Key.MaterialID,
                             Qty = t.qty,
                             SourceBillNo = t.Key.SourceBillNo
                         }).ToList();
            var result = PaperData<PMArrNoticeModel>.Create(query.Count(), Size, Page);
            result.Items = query.OrderByDescending(p => p.BillDate).Skip(result.Skip)
                          .Take(result.Limt).ToList();
            return Success(result);
        }

        #endregion 获取列表

        #endregion 查询
    }

    public partial class PMArrNoticeModel
    {
        public Nullable<System.DateTime> CheckDate { get; set; }
        public string CNStateName { get; set; }
        public string BillNo { get; set; }
        public string MaterialID { get; set; }
        public Nullable<decimal> Qty { get; set; }
        public string SourceBillNo { get; set; }
        public System.DateTime BillDate { get; set; }
    }
}

